<template>
  <div id="myBusinessManagement">
    <h1 class="con-right-title">运营位配置管理</h1>
    <!-- 操作按钮部分 -->
    <div class="table-conBtn-left">
      <el-button
        class="add"
        type="warning"
        icon="el-icon-plus"
        @click="addBtn('add')"
        >新增</el-button
      >
    </div>
    <!-- 表格部分 -->
    <el-table
      id=""
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      max-height="700"
      ref="table"
    >
      <el-table-column width="50" label="序号" align="center">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="运营位名称"></el-table-column>
      <el-table-column
        label="运营位图片"
        align="center"
        prop="view"
        width="110"
        height="60"
      >
        <template slot-scope="scope">
          <listImgShow :imgInfo="scope.row.imgJson"></listImgShow>
        </template>
      </el-table-column>
      <el-table-column
        label="创建人"
        prop="createdUser.trueName"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.createdUser.trueName }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.status === 'OPEN'"
            active-text="开启"
            inactive-text="关闭"
            @change="changeSwitch(scope.row, scope.$index)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="跳转链接" prop="redirectContent" align="center">
      </el-table-column>
      <el-table-column label="排序" prop="sort" align="center">
        <template slot-scope="scope">
          <div
            style="cursor: pointor; background-color: #fff"
            contenteditable="true"
            ref="giftDataNumber"
            @blur="editGiftDataNumber($event, scope.$index)"
            v-text="scope.row.sort"
          ></div>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createdTime"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="220">
        <template slot-scope="scope">
          <el-button
            :class="scope.row.status === 'OPEN' ? 'disabled' : ''"
            :disabled="scope.row.status === 'OPEN'"
            @click="addBtn('edit', scope.row.id, scope.row)"
            type="text"
          >
            编辑
          </el-button>
          <el-button
            :class="scope.row.status === 'OPEN' ? 'disabled' : ''"
            :disabled="scope.row.status === 'OPEN'"
            @click="delActive(scope.row)"
            type="text"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增/编辑弹窗 -->
    <addDialog
      v-if="addDialogData.isShow"
      :addDialogData="addDialogData"
      @refresh="refresh"
    ></addDialog>

    <!-- 弹窗部分 -->
    <dialogSort
      v-if="sortData.isShow"
      :sort-data="sortData"
      @refresh="refresh"
    ></dialogSort>

    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
  </div>
  <!-- 最外层的div 结束 -->
</template>

<script>
import addDialog from '@component/common/businessManagement/addDialog' // 新增/编辑组件
// 引入接口
import {
  getBusinessManagementList,
  enableBusinessManagement,
  disableBusinessManagement,
  sortBusinessManagement,
  delBusinessManagement,
} from '@/api/businessManagement/index'
export default {
  name: '',
  components: {
    addDialog,
  },
  data() {
    return {
      loading: true,
      // 表格数据
      tableData: [],
      // 总条数
      total: 0,
      // 分页数据
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10, // 页面显示条数
      },
      // location	展示位置 HOME_PAGE 首页 可用值:MY,HOME_PAGE
      type: {
        location: 'HOME_PAGE',
      },
      // 传递给新增弹框的数据
      addDialogData: {
        isShow: false, // 控制弹框显示
        params: {}, // 参数
        location: '',
        title: '',
        id: '',
        type: '',
      },
      // 编辑排序 弹窗变量
      sortData: {
        isShow: false, // 默认隐藏
        id: '',
        sort: '',
        total: '',
      },
    }
  },
  mounted() {},
  async created() {
    await this.searchList()
  },
  methods: {
    // 可编辑元素失去焦点时触发判断修改值是否有变更，无变更时不予请求接口，存在变更时传递变更顺序编号至请求接口修改数据
    editGiftDataNumber(e, tableIndex) {
      let reg = /^[0-9]+$/
      if (reg.test(e.target.innerText)) {
        if (this.tableData[tableIndex].sort === parseInt(e.target.innerText))
          return false
        // 排序接口
        sortBusinessManagement(
          this.tableData[tableIndex].id,
          parseInt(e.target.innerText),
          (res) => {
            this.searchList()
            this.$message({
              message: '操作成功',
              type: 'success',
            })
          }
        )
      } else {
        this.$message.error('请确认您输入的排序编号为正整数')
      }
    },
    /* 获取列表 */
    searchList() {
      this.loading = true
      // 位置: MY 我的| HOME_PAGE 首页; 默认 首页,可用值:MY,HOME_PAGE
      let params = { ...this.searchParams, ...this.type }
      getBusinessManagementList(params, (res) => {
        if (res.rows) {
          this.tableData = res.rows
          this.tableData.forEach((item, index) => {
            this.$set(item, 'imgJson', [])
            item.imgJson.push({
              url: item.imgInfo,
            })
          })
          this.total = res.total
        }
        this.loading = false
      })
    },
    // 新增方法
    addBtn(type = '', id = '', row = {}) {
      this.addDialogData.location = 'HOME_PAGE'
      this.addDialogData.isShow = true
      if (id) {
        this.addDialogData.id = id
        this.addDialogData.params = row
      } else {
        this.addDialogData.id = ''
        this.addDialogData.params = {}
      }
    },
    // 开启或关闭状态
    changeSwitch({ id, status }, index) {
      const desc =
        status == 'OPEN' ? '是否关闭该运营位？' : '是否启用该运营位？'
      const title = status == 'OPEN' ? '禁用分类' : '启用分类'
      this.$confirm(desc, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          if (status === 'CLOSE') {
            enableBusinessManagement(id, (res) => {
              this.searchList()
              this.$message({
                message: '操作成功',
                type: 'success',
              })
            })
          } else {
            disableBusinessManagement(id, (res) => {
              this.searchList()
              this.$message({
                message: '操作成功',
                type: 'success',
              })
            })
          }
        })
        .catch(() => {})
    },
    delActive(item) {
      this.$confirm(`是否删除该运营位`, '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        delBusinessManagement(item, (res) => {
          this.searchList()
        })
      })
    },
    /* 刷新列表 */
    async refresh() {
      await this.searchList()
    },
  },
}
</script>

<style lang="less" scoped>
.table-conBtn-left {
  .add {
    float: left;
    margin-left: 30px;
    margin-bottom: 30px;
  }
}
.disabled {
  color: #c0c4cc !important;
}
</style>